const videoModel=require('../model/videoModel');
const path=require('path')
const fs=require('fs');
exports.list=(req,res)=>{
    videoModel.queryAll().then(result=>{
        if(result.length>0){
            res.send({
               data:result,
               mate:{
                  msg:'查询成功',
                  status:200
               }
            })
         }else{
           res.send({
               mate:{
                 msg:'查询失败',
                 status:404
              }
           })
         }
      }).catch(result=>{
        res.send({
            mate:{
               msg:'服务器内部错误',
               status:500,
               error:result
            }
          
         })
      })
   
}
exports.add=(req,res)=>{
   let {video_name,video_info,video_status}=req.body;
  
   let videoname = req.file.originalname;
   let extname=path.extname(req.file.originalname);

   // console.log(`获得的字段为  ${video}`);
   // console.log(`获得的视频为  ${videoname}`);
   // console.log(`获得的视频格式为  ${extname}`);
   // console.log(`视频的原始路径为：${req.file.path}`)
   // res.send('ok!');

   let video=[video_name,video_info,extname,video_status];
   videoModel.insertVideo(video).then(result=>{
      if(result.affectedRows>0){
         fs.rename(req.file.path,`./${req.file.destination}/${result.insertId}${extname}`,err=>{
            if(err){
               res.send({
                  mate:'视频添加失败',
                  status:404
               })
            }else{
               res.send({
                  mate:'视频添加成功',
                  status:200
               })
            }
         })
      }
   }).catch(result=>{
      res.send({
          mate:{
             msg:'服务器内部错误',
             status:500,
             error:result
          }
        
       })
    })
}